package room

import (
	"errors"
	"server/common"

	"github.com/mitchellh/mapstructure"
	"github.com/sirupsen/logrus"
)

// -------------------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------------------
func (s *RoomService) checkStopSubscribeReqData(data common.SFUStopSubscribeReqData) error {
	// Check if room ID provided
	if data.RoomId == "" {
		return errors.New("room ID is required in stop subscribe request")
	}

	// Check if user ID provided
	if data.UserId == "" {
		return errors.New("user ID is required in stop subscribe request")
	}

	// Check media information
	if err := ValidateMediaInfo(data.Media); err != nil {
		return errors.New("invalid media information in stop subscribe request")
	}

	// Check if room exists
	if !s.roomMgr.RoomExists(data.RoomId) {
		return errors.New("room does not exist")
	}

	// Check if user is in the room
	if !s.roomMgr.UserExists(data.RoomId, data.UserId) {
		return errors.New("user is not in the room")
	}

	return nil
}

// -------------------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------------------
func (s *RoomService) sendStopSubscribeResponse(rid string, code int, msg string, connId string,
	data common.SFUStopSubscribeReqData) {

	resData := common.ResMsgData{
		Code: code,
		Msg:  msg,
		Data: common.SFUStopSubscribeResData{
			RoomId: data.RoomId,
			UserId: data.UserId,
			Media:  data.Media,
		},
	}

	resMsg := common.Message{
		Cmd:  common.SFU_STOP_SUBSCRIBE_RES,
		Rid:  rid,
		Data: resData,
	}

	err := s.dispatcher.Dispatch(resMsg, s.serviceInfo, common.AccessServiceInfo, connId)
	if err != nil {
		LOG().WithError(err).Error("failed to dispatch stop subscribe response")
	}
}

// -------------------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------------------
func (s *RoomService) handleStopSubscribeReq(msg common.Message, from common.ServiceInfo,
	connId string) error {
	LOG().WithFields(logrus.Fields{
		"msg":    common.ToJson(msg),
		"from":   from,
		"connId": connId,
	}).Info("Handling SFU Stop Subscribe Request")

	var data common.SFUStopSubscribeReqData
	if err := mapstructure.Decode(msg.Data, &data); err != nil {
		LOG().WithError(err).Error("Failed to decode stop subscribe request data")
		return err
	}

	if err := s.checkStopSubscribeReqData(data); err != nil {
		LOG().WithError(err).Error("Invalid stop subscribe request data")
		s.sendStopSubscribeResponse(msg.Rid, 1, err.Error(), connId, data)
		return nil
	}

	if err := s.roomMgr.CloseConsumer(msg.Rid, data); err != nil {
		LOG().WithError(err).Error("Failed to close consumer")
		s.sendStopSubscribeResponse(msg.Rid, 1, "failed to close consumer", connId, data)
		return nil
	}

	s.sendStopSubscribeResponse(msg.Rid, 0, "success", connId, data)

	return nil
}
